Page({
  data: {
    repairList: [],
    current: 1,
    pageSize: 3,
    finished: false
  },
  onShow() {
    // 获取报修列表
    this.getRepairList()
  },

  bindscrolltolower() {
    console.log('滚动到底部')
    // 不一定要加载, 只有当数据还有下一页的时候可以这么做
    // 只有当 finished 为 false 的情况下才能继续
    if (this.data.finished) return
    // 每当滚动到底部, 将页码 ++ 重新发送请求
    this.setData({
      current: this.data.current + 1
    })
    this.getRepairList()
  },

  async getRepairList() {
    // 请求数据接口
    const { code, data: { rows: repairList }} = await wx.http.get('/repair', { current: this.data.current, pageSize: this.data.pageSize })
    // 检测接口调用的结果
    if (code !== 10000) return wx.utils.toast('获取报修列表失败!')
    // 渲染报修列表
    this.setData({
      repairList: [...this.data.repairList, ...repairList],
      isEnpty: repairList.length === 0
    })

    if (repairList.length < this.data.pageSize) {
      this.setData({
        finished: true
      })
    }
  },

  goDetail(event) {
    console.log(event.mark.id)
    wx.navigateTo({
      url: `/repair_pkg/pages/detail/index?id=${event.mark.id}`
    })
  },
  addRepair() {
    wx.navigateTo({
      url: '/repair_pkg/pages/form/index'
    })
  }
})
